/**
 * @Author: 陈瑞鹏
 * @Date:   2021-11-08 19:19:24
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-10 15:50:19
 */
import netCtr from "../../ctrs/netCtr";

let sdk = cc.Class({

    properties: {
        initdata: null,
    },

    init() {

        let url = "//m.heitu.com/game/qhjssdk";

        let script = document.createElement('script');

        script.src = url;

        script.type = "text/javascript";

        script.onload = this.loadScriptComplete;

        document.getElementsByTagName('head')[0].appendChild(script);

        // this.onCloseWeb();
    },

    loadScriptComplete() {

        sdk.ins().initdata = {
            'username': sdk.ins().getValueByKey('username'),
            'serverid': sdk.ins().getValueByKey('serverid'),
            'time': sdk.ins().getValueByKey('time'),
            'isadult': sdk.ins().getValueByKey('isadult'),
            'uimg': sdk.ins().getValueByKey('uimg'),
            'nname': sdk.ins().getValueByKey('nname'),
            'unid': sdk.ins().getValueByKey('unid'),
            'qhchannel': sdk.ins().getValueByKey('qhchannel'),
            'qhchannelid': sdk.ins().getValueByKey('qhchannelid'),
            'showbtn': sdk.ins().getValueByKey('showbtn'),
            'flag': sdk.ins().getValueByKey('flag'),
            "gid": '4305', //群黑游戏id，可以在后台游戏列表查询
        }

        if (sdk.ins().initdata.username) {
            qhsdk.init(sdk.ins().initdata);

            netCtr.ins.qhlogin();
        }

    },

    /**
     * 上报角色数据
     * @param {*} data 
     */
    upRoleData(roledata) {
        // var roledata = { "act": "1", "serverid": "123", "rolename": "测试123", "roleid": "111", "level": "1", "power": "1" };
        qhsdk.role(roledata);
    },

    getValueByKey(key) {

        let query = window.location.search.substring(1);

        let arr = query.split("&");

        let paramArr = [];

        for (let i = 0; i < arr.length; i++) {

            paramArr = arr[i].split("=");

            if (paramArr[0] == key) {

                return paramArr[1];
            }
        }

        return null;
    },
    payRmb(paydata) {

        qhsdk.pay(paydata, function(code, msg) {
            //充值结果通知，code为编号，msg为信息。该结果不能作为发货依据。该回调已经取消！！请使用后端回调判断发货
            //code=1充值成功 ，其他为充值失败。
            //alert(code+','+msg);
        });
    },

    prohibit() {

        // return;
        //禁止鼠标右击
        document.oncontextmenu = function() {
            event.returnValue = false;
        };
        //禁用开发者工具F12
        document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
            let e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 123) {
                e.returnValue = false;
                return false;
            }
        };
        let userAgent = navigator.userAgent;
        if (userAgent.indexOf("Firefox") > -1) {
            let checkStatus;
            let devtools = /./;
            devtools.toString = function() {
                checkStatus = "on";
            };
            setInterval(function() {
                checkStatus = "off";
                // console.log(devtools);
                // console.log(checkStatus);
                console.clear();
                if (checkStatus === "on") {
                    let target = "";
                    try {
                        window.open("about:blank", (target = "_self"));
                    } catch (err) {
                        let a = document.createElement("button");
                        a.onclick = function() {
                            window.open("about:blank", (target = "_self"));
                        };
                        a.click();
                    }
                }
            }, 200);
        } else {
            //禁用控制台
            let ConsoleManager = {
                onOpen: function() {
                    alert("Console is opened");
                },
                onClose: function() {
                    alert("Console is closed");
                },
                init: function() {
                    let self = this;
                    let x = document.createElement("div");
                    let isOpening = false,
                        isOpened = false;
                    Object.defineProperty(x, "id", {
                        get: function() {
                            if (!isOpening) {
                                self.onOpen();
                                isOpening = true;
                            }
                            isOpened = true;
                            return true;
                        }
                    });
                    setInterval(function() {
                        isOpened = false;
                        console.info(x);
                        console.clear();
                        if (!isOpened && isOpening) {
                            self.onClose();
                            isOpening = false;
                        }
                    }, 200);
                }
            };
            ConsoleManager.onOpen = function() {
                //打开控制台，跳转
                let target = "";
                try {
                    window.open("about:blank", (target = "_self"));
                } catch (err) {
                    let a = document.createElement("button");
                    a.onclick = function() {
                        window.open("about:blank", (target = "_self"));
                    };
                    a.click();
                }
            };
            ConsoleManager.onClose = function() {
                alert("Console is closed!!!!!");
            };
            ConsoleManager.init();
        }

    },

    /**
     * 监听关闭
     */
    onCloseWeb() {

        document.addEventListener("onunload", () => {

            console.log("onunload")
                // alert('onunload')
        })

        document.addEventListener("onbeforeunload", () => {

            console.log("onbeforeunload")
                // alert('onbeforeunload')

        })

        document.addEventListener("visibilitychange", () => {
            if (document.hidden) {
                console.log('页面被挂起')
            } else {
                console.log('页面呼出')
            }
            // alert('visibilitychange')
        });

    }
})

sdk._instance = null;

sdk.ins = function() {

    if (sdk._instance == null) {

        sdk._instance = new sdk();
    }

    return sdk._instance;
}

module.exports = sdk;